package com.shisfish.library.Controller;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;

import javax.ws.rs.core.*;

import javax.ws.rs.ext.Provider;

import net.sf.json.JSONArray;

import org.codehaus.jackson.map.ObjectMapper;

import com.shisfish.core.Model.TestBean;
import com.shisfish.library.Utils.FileUpload.FileUploadUtils;
import com.shisfish.library.Utils.FileUpload.FormJsonArrUtil;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;


@Provider
@Path("file")
public class FileUploadController {

	@POST
	@Path("/upload")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	public Response uploadFile(
			@FormDataParam("file") InputStream uploadedInputStream,
			@FormDataParam("file") FormDataContentDisposition fileDetail) {
		String uploadedFileLocation = "d://" + fileDetail.getFileName();
		// save it
		writeToFile(uploadedInputStream, uploadedFileLocation);
		String output = "File uploaded to : " + uploadedFileLocation;
		return Response.status(200).entity(output).build();
	}

	// save uploaded file to new location
	private void writeToFile(InputStream uploadedInputStream,
			String uploadedFileLocation) {

		try {
			OutputStream out = new FileOutputStream(new File(
					uploadedFileLocation));
			int read = 0;
			byte[] bytes = new byte[1024];

			out = new FileOutputStream(new File(uploadedFileLocation));
			while ((read = uploadedInputStream.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}
			out.flush();
			out.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
	
	
	@POST
	@Path("/demoData")
	@Consumes("application/json")
	@Produces("application/json")
	public String getDemoData(HashMap<String, String> params) throws Exception {
		List<TestBean> list = new ArrayList<TestBean>();
		TestBean bean = new TestBean();
		String id = (String) params.get("id");
		bean.setId(id);
		String name = (String) params.get("name");
		bean.setName(name);
		String birthday = (String) params.get("birthday");
		bean.setBirthday(birthday);
		list.add(bean);
		bean = new TestBean();
		bean.setId("222");
		bean.setName("xiao zhang");
		list.add(bean);
		ObjectMapper objectMapper = new ObjectMapper();
		String listJson = objectMapper.writeValueAsString(list);
		return listJson;
	}

	
	/**
	 * 多文件多表单值上传例子
	 * @param request
	 * @return
	 */
	@POST
	@Path("/getRiverWayUpload")
	@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
	@Produces(MediaType.TEXT_PLAIN)
	public String getRiverWayUpload(@Context HttpServletRequest request) {
		//System.out.println(token);
		FileUploadUtils fuu = new FileUploadUtils();
		JSONArray rejsArr;
		fuu.setFilePath("d:\\");
		//取得所有文件和表单值
		JSONArray jsArr = fuu.getRiverWayUpload(request);
		
		//取得所有普通表单值
		rejsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "form");
		//System.out.println("取得所有普通表单值:"+rejsArr.toString());
		
		//取得所有文件值
		rejsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "file");
		//System.out.println("取得所有文件值:"+rejsArr.toString());
		
		return rejsArr.toString();
	}
}
